import pymysql
from tools import settings
class MysqlClient:
    def __init__(self):

        self.connection = pymysql.connect(
            host=settings.DATABASE.get("host"),
            port=settings.DATABASE.get("port"),
            user=settings.DATABASE.get("user"),
            password=settings.DATABASE.get("password"),
            database=settings.DATABASE.get("database"),
        )
        # 创建游标对象
        self.cursor = self.connection.cursor()

    def execute(self, sql, args=None):
        """执行sql"""
        res = self.cursor.execute(sql, args)
        return res

    def fetch_one(self):
        """获取一条数据"""
        return self.cursor.fetchone()

    def fetch_all(self):
        """获取所有数据"""
        return self.cursor.fetchall()

    def close(self):
        self.cursor.close()
        self.connection.close()
